﻿package mortal.game.view.treasureMap
{
    import Message.Game.*;
    import Message.Public.*;
    import com.fyGame.fyMap.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.display.*;
    import mortal.common.global.*;
    import mortal.common.net.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.activeTime.*;
    import mortal.game.view.common.*;
    import mortal.game.view.vip.*;
    import mortal.game.view.vip.panel.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class TreasureMapModule extends Window
    {
        private var _bg:GImageBitmap;
        private var _midBg:ScaleBitmap;
        private var _btnVipSend:GLoadingButton;
        private var _btnSearch:GLoadingButton;
        private var _btnDigging:GLoadingButton;
        private var _btnGiveup:GLoadingButton;
        private var _MapImage:GBitmap;
        private var _target:GImageBitmap;
        private var _mapSprite:GSprite;
        private var _doubleCountDown:DoubleCountDownBox;
        private var _mask:GBitmap;
        private var _mapInfo:GMapInfo;
        private var _mapText:GTextFiled;
        private var _vipOpenTips:VipOpenTipsPanel;
        public static var doubleTreasureActive:SClientActive;

        public function TreasureMapModule(param1:ILayer = null)
        {
            super(param1);
            this.setSize(833, 554);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._titleSpriteHight = 60;
            this._midBg = UIFactory.bg(90, 56, 660, 415, this, ImagesConst.WizardBg);
            this._mapSprite = UICompomentPool.getUICompoment(GSprite);
            this._mapSprite.x = 90;
            this._mapSprite.y = 60;
            this.addChild(this._mapSprite);
            this._MapImage = UIFactory.bitmap("", 0, 0, this._mapSprite);
            this._bg = UIFactory.gImageBitmap(ImagesConst.TreasureMapBg, 0, -17, this);
            this._target = UIFactory.gImageBitmap(ImagesConst.TreasureMapTarget, 0, 0, this._mapSprite);
            this._doubleCountDown = UICompomentPool.getUICompoment(DoubleCountDownBox);
            this._doubleCountDown.countDownUrl = ImagesConst.DoubleContdowmTreasure;
            this._doubleCountDown.startUrl = ImagesConst.doubleActiveRes_Treasuring;
            this._doubleCountDown.endUrl = ImagesConst.doubleActiveRes_TreasureEnd;
            this._doubleCountDown.x = 293;
            this._doubleCountDown.y = 363;
            this.addChild(this._doubleCountDown);
            this._btnVipSend = UIFactory.gLoadingButton(ResFileConst.vipSentBtn, 135, 476, 110, 44, this);
            this._btnVipSend.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._btnVipSend.toolTipData = Language.getStringByParam(35580, VipUtil.getVipMinLevelByType(EVipRecordType._EVipRecordTypeTreasureMapPass));
            this._btnSearch = UIFactory.gLoadingButton(ResFileConst.TreasureMapSearch, 285, 476, 110, 44, this);
            this._btnSearch.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._btnDigging = UIFactory.gLoadingButton(ResFileConst.TreasureMapDigging, 435, 476, 110, 44, this);
            this._btnDigging.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._btnGiveup = UIFactory.gLoadingButton(ResFileConst.TreasureMapGiveUp, 585, 476, 110, 44, this);
            this._btnGiveup.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this.pushUIToDisposeVec(UIFactory.bg(110, 70, 150, 25, this, ImagesConst.ComboBox));
            this._mapText = UIFactory.gTextField("", 121, 72, 130, 23, this, GlobalStyle.textFormatBai.center());
            this._mask = GlobalClass.getBitmap(BitmapDataConst.LockedBMD);
            this._mask.width = 660;
            this._mask.height = 406;
            this._mapSprite.addChild(this._mask);
            this._mapSprite.mask = this._mask;
            this._vipOpenTips = UICompomentPool.getUICompoment(VipOpenTipsPanel);
            this._vipOpenTips.move(this._btnVipSend.x - 67, this._btnVipSend.y - 30);
            this._vipOpenTips.valueType = VipOpenTipsPanel.noValue;
            this._vipOpenTips.resetLevelTextPos(63, 9, 81, 9);
            this._vipOpenTips.vipType = EVipRecordType._EVipRecordTypeTreasureMapPass;
            this.addChild(this._vipOpenTips);
            var _loc_1:* = this.getImageInfo();
            if (_loc_1)
            {
                LoaderManager.instance.loadInfo(_loc_1, this.onLoaded);
            }
            this.updateVip();
            this.updateLeftTime();
            return;
        }// end function

        private function updateLeftTime() : void
        {
            this._doubleCountDown.sClientActive = ActiveTimeUtil.getSClientActive(EActiveType._EActiveTypeTreasureMap);
            return;
        }// end function

        private function onLoaded(param1:MapImgInfo = null) : void
        {
            if (isDisposed)
            {
                return;
            }
            var _loc_2:* = param1.bitmapData;
            this._MapImage.bitmapData = _loc_2;
            this._MapImage.width = 660;
            this._MapImage.height = 660 / _loc_2.width * _loc_2.height;
            this._MapImage.smoothing = true;
            MapLoader.instance.loadMapData(Cache.instance.treasureMap.pos.map, this.onMapIdLoadedHandler);
            return;
        }// end function

        private function onMapIdLoadedHandler(param1:FyMapInfo) : void
        {
            this._target.x = this._MapImage.width * Cache.instance.treasureMap.pos.x / param1.gridWidth - this._target.width * 0.5;
            this._target.y = this._MapImage.height * Cache.instance.treasureMap.pos.y / param1.gridHeight - this._target.height * 0.5;
            if (this._target.y >= 406 - this._target.height * 0.5)
            {
                if (this._MapImage.height - this._target.y < 203)
                {
                    this._MapImage.y = -this._MapImage.height + 403;
                }
                else
                {
                    this._MapImage.y = -(this._target.y - 203);
                }
            }
            else
            {
                this._MapImage.y = 0;
            }
            this._target.y = this._target.y + this._MapImage.y;
            return;
        }// end function

        private function getImageInfo() : ResourceInfo
        {
            var _loc_1:* = Cache.instance.treasureMap.pos.map + "_mini.jpg";
            var _loc_2:* = new MapImgInfo();
            _loc_2.name = _loc_1;
            _loc_2.mapID = Cache.instance.treasureMap.pos.map;
            var _loc_3:* = _loc_1;
            this._mapInfo = GameMapConfig.instance.getMapInfo(_loc_2.mapID);
            _loc_2.loaclPath = PathConst.mapLocalPath.substr(0, (PathConst.mapLocalPath.length - 1)) + "_min/" + _loc_3;
            _loc_2.time = this._mapInfo.version;
            _loc_2.path = PathConst.mapPath.substr(0, (PathConst.mapPath.length - 1)) + "_min/" + _loc_3 + "?v=" + _loc_2.time;
            _loc_2.type = ".jpg";
            ResourceManager.addResource(_loc_2);
            this._mapText.text = this._mapInfo.name + "(" + Cache.instance.treasureMap.pos.x + "," + Cache.instance.treasureMap.pos.y + ")";
            return _loc_2;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            EffectManager.glowFilterUnReg(this._btnDigging);
            this._bg.dispose(param1);
            this._bg = null;
            this._btnSearch.dispose(param1);
            this._btnSearch = null;
            this._btnDigging.dispose(param1);
            this._btnDigging = null;
            this._btnGiveup.dispose(param1);
            this._btnGiveup = null;
            this._midBg.dispose(param1);
            this._midBg = null;
            this._target.dispose(param1);
            this._target = null;
            this._mapText.dispose(param1);
            this._mapText = null;
            this._MapImage.dispose(param1);
            this._MapImage = null;
            this._mapSprite.mask = null;
            this._mapSprite.dispose(param1);
            this._mapSprite = null;
            this._mask.dispose(param1);
            this._mask = null;
            this._btnVipSend.dispose(param1);
            this._btnVipSend = null;
            if (this._doubleCountDown)
            {
                this._doubleCountDown.dispose(param1);
                this._doubleCountDown = null;
            }
            if (this._vipOpenTips)
            {
                this._vipOpenTips.dispose(param1);
                this._vipOpenTips = null;
            }
            this._mapInfo = null;
            super.disposeImpl(param1);
            return;
        }// end function

        private function useAndBuyFlyBoot() : void
        {
            var _loc_1:* = Cache.instance.treasureMap.pos;
            var _loc_2:* = new SPassTo();
            _loc_2.toPoint = new SPoint();
            _loc_2.toPoint.x = _loc_1.x;
            _loc_2.toPoint.y = _loc_1.y;
            _loc_2.mapId = _loc_1.map;
            AIManager.cancelAll();
            AIManager.addFlyBoot(_loc_2);
            AIManager.addCallback(this.latterShowModule);
            AIManager.start();
            this.hide();
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            var _loc_3:SPos = null;
            var _loc_4:SPos = null;
            var _loc_2:* = event.target as DisplayObject;
            switch(_loc_2)
            {
                case this._btnVipSend:
                {
                    if (!VipConfig.instance.isShowEffect(EVipRecordType._EVipRecordTypeTreasureMapPass, Cache.instance.vip.level))
                    {
                        return MsgManager.showRollTipsMsg(Language.getStringByParam(35580, VipUtil.getVipMinLevelByType(EVipRecordType._EVipRecordTypeTreasureMapPass)));
                    }
                    _loc_3 = Cache.instance.treasureMap.pos;
                    if (RolePlayer.instance.x2d == _loc_3.x && RolePlayer.instance.y2d == _loc_3.y)
                    {
                        return MsgManager.showRollTipsMsg(Language.getString(35581));
                    }
                    if (!FlyBootAI.checkCanFly())
                    {
                        return;
                    }
                    GameController.guide.checkAndBuyFlyBoot(this.useAndBuyFlyBoot);
                    return;
                }
                case this._btnSearch:
                {
                    _loc_4 = Cache.instance.treasureMap.pos;
                    if (RolePlayer.instance.x2d == _loc_4.x && RolePlayer.instance.y2d == _loc_4.y)
                    {
                        return MsgManager.showRollTipsMsg(Language.getString(35581));
                    }
                    AIManager.cancelAll();
                    AIManager.addMoveToOtherMap(Game.mapInfo.mapId, _loc_4.map, new Point(_loc_4.x, _loc_4.y), 0, this.setShowModule);
                    AIManager.start();
                    break;
                }
                case this._btnDigging:
                {
                    AIManager.cancelAll();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TreasureDigging, null));
                    break;
                }
                case this._btnGiveup:
                {
                    AIManager.cancelAll();
                    if (TreasureMapProgressView.isInProgress)
                    {
                        TreasureMapProgressView.instance.hide();
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TreasureMapOper, EDigTreasureOperate._EDiscardMap));
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.hide();
            return;
        }// end function

        private function setShowModule() : void
        {
            AIManager.addCallback(this.showModule);
            return;
        }// end function

        private function latterShowModule() : void
        {
            CallLater.setCallLater(this.showModule, 1);
            return;
        }// end function

        private function showModule(param1:Object = null) : void
        {
            this.show();
            EffectManager.glowFilterReg(this._btnDigging);
            return;
        }// end function

        private function digTreasure() : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.TreasureMapOper, EDigTreasureOperate._EDigTreasure));
            if (TreasureMapProgressView.isInProgress)
            {
                TreasureMapProgressView.instance.hide();
            }
            return;
        }// end function

        override protected function updateBtnSize() : void
        {
            if (_closeBtn)
            {
                _closeBtn.x = this.width - _closeBtn.width - 22;
                _closeBtn.y = 2;
                _contentTopOf3DSprite.addChild(_closeBtn);
            }
            return;
        }// end function

        public function updateVip() : void
        {
            if (VipConfig.instance.isShowEffect(EVipRecordType._EVipRecordTypeTreasureMapPass, Cache.instance.vip.level))
            {
                this._btnVipSend.filters = [];
            }
            else
            {
                this._btnVipSend.filters = [FilterConst.colorFilter2];
            }
            return;
        }// end function

    }
}
